package me.gavin.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import me.gavin.BR

abstract class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView()
        afterCreate(savedInstanceState)
    }

    /**
     * AutoSize 在任何情况下本来适配正常的布局突然出现适配失效 的 万能解决方案
     * @see https://github.com/JessYanCoding/AndroidAutoSize/issues/13
     */
    override fun getResources() = super.getResources().fixInvalidOnAny(this)

    abstract fun setContentView()

    abstract fun afterCreate(savedInstanceState: Bundle?)

    open fun doSomething() {} // 预留

}

abstract class BindingActivity<B : ViewDataBinding> : BaseActivity() {

    lateinit var binding: B
        private set

    abstract val layoutId: Int

    override fun setContentView() {
        binding = DataBindingUtil.setContentView(this, layoutId)
        binding.lifecycleOwner = this // TODO
    }

    override fun onDestroy() {
        super.onDestroy()
        binding.unbind() // TODO 需要吗
    }

}

abstract class BindingVMActivity<B : ViewDataBinding> : BindingActivity<B>() {

    abstract val viewModel: BaseViewModel

    override fun setContentView() {
        super.setContentView()
        binding.setVariable(BR.vm, viewModel)
    }

}